#include "gtlFormats.h"
#include "gtlFiles.h"

namespace GTL
{
	namespace Formats
	{
		Structures::DataNode* GetNewNode(int format)
		{
			switch (format)
			{
			case 'wils':
				return new Wilson::DataNodeType;
			case 'genc':
				return new GEncode::DataNodeType;
			default:
				return 0;
			}
		}

		int DetectFormatFromFilename( const char* filename ) 
		{
			const char* ext = GTL::Files::GetFileExtension(filename);
			if (!stricmp("wilson", ext))
				return 'wils';
			else if (!stricmp("gencode", ext))
				return 'genc';

			return 0;
		}

		Structures::DataNode* LoadNodeFromFile( const char* filename, int fileformat )
		{
			if (fileformat == GTL::Formats::AutoDetect::FormatID)
				fileformat = GTL::Formats::DetectFormatFromFilename(filename);
			if (!fileformat) return 0;

			GTL::Structures::DataNode* node = GTL::Formats::GetNewNode(fileformat);
			char* tf = GTL::Files::Just::LoadEntireTextFile(filename);
			node->Parse(tf);
			delete[] tf;
			return node;
		}
	}
}